package com.ly.lypopupwindowdemo.util;

import android.content.Context;

/**
 * Created by liying on 2017/7/19.
 * <p>
 * http://blog.csdn.net/jiangwei0910410003/article/details/40509571
 */

public class SystemUtils {

    private static final int CHINESE = 1;
    private static final int NUMBER_OR_CHARACTER = 2;


    private static Context mContext;


    /**
     * 代码里面进行转化的时候还需要有一个偏移值：0.5f
     */

    private static final float scale = mContext.getResources().getDisplayMetrics().density;

    private static final float scaledDensity = mContext.getResources().getDisplayMetrics().scaledDensity;

    private static int dip2px(float dipValue) {
        return (int) (dipValue * scale + .5f);
    }

    /**
     * px转成dp
     *
     * @param pxValue
     * @return
     */
    public static int px2dip(float pxValue) {
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * sp转成px
     *
     * @param spValue
     * @param type
     * @return
     */
    public static float sp2px(float spValue, int type) {
        switch (type) {
            case CHINESE:
                return spValue * scaledDensity;
            case NUMBER_OR_CHARACTER:
                return spValue * scaledDensity * 10.0f / 18.0f;
            default:
                return spValue * scaledDensity;
        }
    }
}
